{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20-1-13 是这一年的第 13 天。\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "def day_of_year(year, month, day):\n",
    "    date = datetime(year, month, day)\n",
    "    return (date - datetime(year, 1, 1)).days + 1\n",
    "\n",
    "year = int(input(\"请输入年份: \"))\n",
    "month = int(input(\"请输入月份: \"))\n",
    "day = int(input(\"请输入日期: \"))\n",
    "\n",
    "print(f\"{year}-{month}-{day} 是这一年的第 {day_of_year(year, month, day)} 天。\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 到 1000 之间有 168 个素数。\n",
      "所有素数为：\n",
      "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]\n"
     ]
    }
   ],
   "source": [
    "# 判断一个数是否为素数\n",
    "def is_prime(n):\n",
    "    if n <= 1:\n",
    "        return False\n",
    "    for i in range(2, int(n**0.5) + 1):  # 优化：只需检查到平方根\n",
    "        if n % i == 0:\n",
    "            return False\n",
    "    return True\n",
    "\n",
    "# 计算并输出 1 到 1000 之间的素数\n",
    "primes = []\n",
    "for number in range(1, 1001):\n",
    "    if is_prime(number):\n",
    "        primes.append(number)\n",
    "\n",
    "# 输出素数个数和所有素数\n",
    "print(f\"1 到 1000 之间有 {len(primes)} 个素数。\")\n",
    "print(\"所有素数为：\")\n",
    "print(primes)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该学生的成绩等级是：A\n"
     ]
    }
   ],
   "source": [
    "# 输入成绩\n",
    "score = float(input(\"请输入学生成绩：\"))\n",
    "\n",
    "# 使用嵌套的条件运算符判断成绩等级\n",
    "grade = 'A' if score >= 90 else 'B' if score >= 60 else 'C'\n",
    "\n",
    "# 输出结果\n",
    "print(f\"该学生的成绩等级是：{grade}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "英文字母个数：0\n",
      "空格个数：0\n",
      "数字个数：2\n",
      "其它字符个数：0\n"
     ]
    }
   ],
   "source": [
    "# 输入一行字符串\n",
    "input_string = input(\"请输入一行字符串：\")\n",
    "\n",
    "# 初始化计数器\n",
    "letter_count = 0\n",
    "space_count = 0\n",
    "digit_count = 0\n",
    "other_count = 0\n",
    "\n",
    "# 遍历每个字符进行判断\n",
    "for char in input_string:\n",
    "    if char.isalpha():  # 判断是否为英文字母\n",
    "        letter_count += 1\n",
    "    elif char.isdigit():  # 判断是否为数字\n",
    "        digit_count += 1\n",
    "    elif char.isspace():  # 判断是否为空格\n",
    "        space_count += 1\n",
    "    else:  # 其它字符\n",
    "        other_count += 1\n",
    "\n",
    "# 输出结果\n",
    "print(f\"英文字母个数：{letter_count}\")\n",
    "print(f\"空格个数：{space_count}\")\n",
    "print(f\"数字个数：{digit_count}\")\n",
    "print(f\"其它字符个数：{other_count}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1000 以内的完数有： [6, 28, 496]\n"
     ]
    }
   ],
   "source": [
    "# 函数：计算一个数的因子之和（不包括该数本身）\n",
    "def sum_of_factors(n):\n",
    "    factors_sum = 0\n",
    "    for i in range(1, n // 2 + 1):  # 只需遍历到 n//2，因为大于 n//2 的数不可能是 n 的因子\n",
    "        if n % i == 0:  # 如果 i 是 n 的因子\n",
    "            factors_sum += i\n",
    "    return factors_sum\n",
    "\n",
    "# 遍历 1 到 1000 之间的所有数，找出完数\n",
    "perfect_numbers = []\n",
    "for num in range(1, 1001):\n",
    "    if sum_of_factors(num) == num:  # 如果因子之和等于该数\n",
    "        perfect_numbers.append(num)\n",
    "\n",
    "# 输出完数\n",
    "print(\"1000 以内的完数有：\", perfect_numbers)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 + 2! + 3! + ... + 50! = 31035053229546199656252032972759319953190362094566672920420940313\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "\n",
    "# 输入一个数 N\n",
    "N = int(input(\"请输入一个整数 N: \"))\n",
    "\n",
    "# 计算 1 + 2! + 3! + ... + N!\n",
    "result = 0\n",
    "for i in range(1, N + 1):\n",
    "    result += math.factorial(i)\n",
    "\n",
    "# 输出结果\n",
    "print(f\"1 + 2! + 3! + ... + {N}! = {result}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      *\n",
      "     ***\n",
      "    *****\n",
      "   *******\n",
      "  *********\n",
      " ***********\n",
      "*************\n",
      " ***********\n",
      "  *********\n",
      "   *******\n",
      "    *****\n",
      "     ***\n",
      "      *\n"
     ]
    }
   ],
   "source": [
    "# 输入奇数 N\n",
    "N = int(input(\"请输入一个奇数 N: \"))\n",
    "\n",
    "# 判断输入是否为奇数\n",
    "if N % 2 == 0:\n",
    "    print(\"请输入一个奇数！\")\n",
    "else:\n",
    "    # 上半部分：从第 1 行到第 N//2 + 1 行\n",
    "    for i in range(1, N + 1, 2):\n",
    "        spaces = (N - i) // 2  # 计算空格数量\n",
    "        print(' ' * spaces + '*' * i)  # 打印空格和星号\n",
    "    \n",
    "    # 下半部分：从第 N//2 行到第 1 行（倒序打印）\n",
    "    for i in range(N - 2, 0, -2):\n",
    "        spaces = (N - i) // 2  # 计算空格数量\n",
    "        print(' ' * spaces + '*' * i)  # 打印空格和星号\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "排序后的结果是: [52]\n"
     ]
    }
   ],
   "source": [
    "# 输入10个数\n",
    "numbers = list(map(int, input(\"请输入10个数字，空格分隔: \").split()))\n",
    "\n",
    "# 对数字进行排序\n",
    "numbers.sort()\n",
    "\n",
    "# 输出排序结果\n",
    "print(\"排序后的结果是:\", numbers)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "插入后的数组: [1, 3, 4, 5, 7, 9, 11, 13, 15, 17, 19]\n"
     ]
    }
   ],
   "source": [
    "import bisect\n",
    "\n",
    "# 假设已经有一个排好序的数组\n",
    "arr = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]\n",
    "\n",
    "# 从键盘输入一个数字\n",
    "num = int(input(\"请输入要插入的数字: \"))\n",
    "\n",
    "# 使用 bisect.insort 插入新的数字到已排序的数组中\n",
    "bisect.insort(arr, num)\n",
    "\n",
    "# 输出插入后的数组\n",
    "print(\"插入后的数组:\", arr)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最后留下的是原来第 14 号的那位。\n"
     ]
    }
   ],
   "source": [
    "def josephus(n):\n",
    "    # 从0开始的编号\n",
    "    result = 0  # 只有1个人时，位置是0\n",
    "    for i in range(2, n + 1):  # 从2个人开始计算\n",
    "        result = (result + 3) % i\n",
    "    return result + 1  # 返回1开始的编号\n",
    "\n",
    "# 输入人数n\n",
    "n = int(input(\"请输入总人数n: \"))\n",
    "\n",
    "# 输出最后剩下的人的编号\n",
    "print(\"最后留下的是原来第\", josephus(n), \"号的那位。\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
